
  <template>
  <div class="bf-commont-template">
    <!--出行申请-->
    <div class="clearfix bf-top-button">
      <div class="float-l">
        <el-button @click="showAdd" type="primary">发起申请</el-button>
      </div>
      <div class="float-r">
        <div class="float-l">
          <el-input placeholder="请输入申请名称搜索内容" prefix-icon="el-icon-search" v-model="searchData" @keyup.enter.native="quickSearch"></el-input>
        </div>
        <div class="float-l padding-l">
          <el-button @click="showSearch" type="primary">高级筛选</el-button>
        </div>
      </div>
    </div>

    <bf-table :data="userData" :setting="tableListSetting" :rowAction="rowAction" @pageChange="pageChange" :editRules="addRules" :editFormWidth="800" :detailFormWidth="800" ref="table"></bf-table>

    <!-- 添加 -->
    <bf-table-add :width="800" :rowNum="2" :submitButtons="addSubmitButtons" :editForm='addlistSetting' ref='addRosterRef' @formSubmit='addSubmit' :editRules="addRules">
      <template slot="customer">
        <div class="bf-table-title clearfix">
          <div>目标目的地</div>
          <div @click="addCustomer">新增</div>
        </div>
        <bf-table :data="editCustomerData" :setting="customerSetting" :rowAction="editCustomerAction" :hiddenPagination="true" ref="customer"></bf-table>

      </template>

    </bf-table-add>
    <!-- 高级搜索 -->
    <bf-table-search :editForm='searchSetting' ref='searchRosterRef' @formSubmit='searchRosterSubmit'></bf-table-search>

    <!-- 详情 -->
    <bf-table-detail :width="800" :rowNum="2" ref="bfTableDetail" :editForm="listSetting" :showHistory='true' :approveSetting='settingHis'>
      <template slot="customer">
        <div class="bf-table-title clearfix">
          <div>目标目的地</div>
          <div></div>
        </div>
        <bf-table :data="editCustomerData" :setting="customerSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
      <template slot="realcustomer">
        <div class="bf-table-title clearfix">
          <div>实际目的地</div>
          <div></div>
        </div>
        <bf-table :data="realcustomerData" :setting="customerSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
      <template slot="costlist">
        <div class="bf-table-title clearfix">
          <div>报销费用</div>
          <div></div>
        </div>
        <bf-table :data="costlistData" :setting="costlistSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
    </bf-table-detail>

    <!-- 编辑 -->
    <bf-table-edit :width="800" :rowNum="2" :submitButtons="editSubmitButtons" :editForm='listSetting' ref='editRosterRef' @formSubmit='editRosterSubmit' :editRules="addRules">
      <template slot="customer">
        <div class="bf-table-title clearfix">
          <div>目标目的地</div>
          <div @click="addCustomer">新增</div>
        </div>
        <bf-table :data="editCustomerData" :setting="customerSetting" :rowAction="editCustomerAction" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
    </bf-table-edit>

    <!-- 编辑-新增 -->
    <bf-dialog-form ref="editDialogFormAdd" :editRules="editCustomerRules" :editForm="customerSetting" :title="'新增目标目的地'" @formSubmit="submitAdd"></bf-dialog-form>
    <!-- 编辑-编辑 -->
    <bf-dialog-form ref="editdialogFormEdit" :editRules="editCustomerRules" :editForm="customerEditSetting" :title="'编辑目标目的地'" @formSubmit="submitCustomEdit"></bf-dialog-form>

    <!--报销编辑-->
    <bf-table-edit :title="'填写报销申请'" :width="800" :rowNum="2" ref="bfEditCost" :submitButtons="editCostSubmitButtons" :editForm="costEditListSetting">
      <template slot="customer">
        <div class="bf-table-title clearfix">
          <div>目标目的地</div>
          <div></div>
        </div>
        <bf-table :data="editCustomerData" :setting="customerSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
      <template slot="realcustomer">
        <div class="bf-table-title clearfix">
          <div>实际目的地</div>
          <div @click="addRealCustomer">新增</div>
        </div>
        <bf-table :data="realcustomerData" :rowAction="realCustomerAction" :setting="customerSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
      <template slot="costlist">
        <div class="bf-table-title clearfix">
          <div>报销费用</div>
          <div @click="addCost">新增</div>
        </div>
        <bf-table :data="costlistData" :rowAction="FreeAction" :setting="costlistSetting" :hiddenPagination="true" ref="customer"></bf-table>
      </template>
    </bf-table-edit>

    <!-- 实际客户编辑-新增 -->
    <bf-dialog-form ref="editDialogFormRealAdd" :editRules="editCustomerRules" :editForm="customerSetting" :title="'新增实际目的地'" @formSubmit="submitRealAdd"></bf-dialog-form>
    <!-- 实际客户报销编辑-编辑 -->
    <bf-dialog-form ref="editdialogFormRealEdit" :editRules="editCustomerRules" :editForm="customerEditSetting" :title="'编辑实际目的地'" @formSubmit="submitCustomRealEdit"></bf-dialog-form>

    <!-- 报销编辑-新增 -->
    <bf-dialog-form ref="editDialogFormCostAdd" :editRules="addCostRules" :editForm="costlistSetting" :title="'新增报销费用'" @formSubmit="submitCostAdd"></bf-dialog-form>
    <!-- 报销编辑-编辑 -->
    <bf-dialog-form ref="editdialogFormCostEdit" :editRules="addCostRules" :editForm="costEditSetting" :title="'编辑报销费用'" @formSubmit="submitCostEdit"></bf-dialog-form>
  </div>
</template>

<script>
import { searchSetting, tableListSetting, addlistSetting, listSetting, detailSetting, reimbursementDetailSetting, costEditListSetting } from './table.setting.js'
import {
  getOutSideDataApi,
  getListApi,
  editOutApplyApi,
  addOutApplyApi,
  getEmoApi,
  deleteOutApplyApi,
  updateBack,
  giveupdata,
  sendOutApplyApi,
  updateBackCost,
  sendEditOutApplyApi,
  giveupCost,
  deleteCost
} from './api.js'
export default {
  data() {
    return {
      settingHis: {
        procDefKey: '',
        businessKey: ''
      },
      realcustomerData: [], // 实际客户
      costlistData: [], // 报销费用
      editCustomerData: [], // 目标客户
      submitCustDate: [], // 提交的客户数据
      custData: [],
      costEditListSetting: costEditListSetting,
      costlistSetting: [
        {
          field: 'id',
          title: 'id',
          type: 'text',
          disableEdit: true,
          isHiden: true,
          hideDetail: true,
          hideEdit: true
        },

        {
          field: 'costDate',
          title: '日期',
          type: 'date'
        },
        {
          field: 'costType',
          title: '费用类型',
          type: 'selected',
          isParams: true,
          paramsId: 'COSTTYPE'
        },
        {
          field: 'cost',
          title: '费用',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],


      costEditSetting: [
        {
          field: 'id',
          title: 'id',
          type: 'text',
          disableEdit: true,
          isHiden: true,
          hideDetail: true,
          hideEdit: true
        },

        {
          field: 'costDate',
          title: '日期',
          disableEdit: true,
          type: 'date'
        },
        {
          field: 'costType',
          title: '费用类型',
          disableEdit: true,
          type: 'selected',
          isParams: true,
          paramsId: 'COSTTYPE'
        },
        {
          field: 'cost',
          title: '费用',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }
      ],

      editCustomerRules: {
        cusNo: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        cusName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        linkmanTel: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        cusAddr: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        cusLinkman: [
          { required: true, message: '必填项', trigger: 'blur' }
        ]
      },


      addCostRules: {
        costDate: [
          { required: true, message: '请选择日期', trigger: 'blur' }
        ],
        costType: [
          { required: true, message: '请选择报销类型', trigger: 'blur' }
        ],
        cost: [
          { required: true, message: '请输入报销费用', trigger: 'blur' }
        ]
      },
      editCustomerAction: [
        {
          title: '编辑',
          click: (item) => {
            this.$refs.editdialogFormEdit.show(item.scope.row)
          }
        },
        {
          title: '删除',
          click: (item) => {
            const form = item.scope.row
            if (form._addTimeId) {
              this.editCustomerData.forEach((v, i) => {
                if (v._addTimeId == form._addTimeId) {
                  this.editCustomerData.splice(i, 1)
                }
              })
            }
            if (form.id) {
              this.editCustomerData.forEach((v, i) => {
                if (v.id == form.id) {
                  this.editCustomerData.splice(i, 1)
                }
              })
            }
          }
        }
      ],

      // 实际客户
      realCustomerAction: [
        {
          title: '编辑',
          click: (item) => {
            this.$refs.editdialogFormRealEdit.show(item.scope.row)
          }
        },
        {
          title: '删除',
          click: (item) => {
            const form = item.scope.row
            if (form._addTimeId) {
              this.realcustomerData.forEach((v, i) => {
                if (v._addTimeId == form._addTimeId) {
                  this.realcustomerData.splice(i, 1)
                }
              })
            }
            if (form.id) {
              this.realcustomerData.forEach((v, i) => {
                if (v.id == form.id) {
                  this.realcustomerData.splice(i, 1)
                }
              })
            }
          }
        }
      ],
      FreeAction: [
        {
          title: '编辑',
          click: (item) => {
            this.$refs.editdialogFormCostEdit.show(item.scope.row)
          }
        },
        {
          title: '删除',
          click: (item) => {
            const form = item.scope.row
            if (form._addTimeId) {
              this.costlistData.forEach((v, i) => {
                if (v._addTimeId == form._addTimeId) {
                  this.costlistData.splice(i, 1)
                }
              })
            }



            if (form.id) {
              this.costlistData.forEach((v, i) => {
                if (v.id == form.id) {
                  this.costlistData.splice(i, 1)
                }
              })
            }
          }
        }
      ],
      customerSetting: [
        {
          field: 'id',
          title: 'id',
          type: 'text',
          hideDetail: true,
          hideEdit: true,
          isHiden: true,
        },
        {
          field: 'addrName',
          title: '目的地名称',
          type: 'text'
        },
        {
          field: 'addr',
          title: '详细地址',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }, {
          field: 'flag',
          title: '标志',
          type: 'text',
          isHiden: true,
          hideDetail: true,
          hideEdit: true
        }
      ],

      customerEditSetting: [
        {
          field: 'id',
          title: 'id',
          type: 'text',
          hideDetail: true,
          hideEdit: true,
          isHiden: true,
        },
        {
          field: 'addrName',
          title: '目的地名称',
          type: 'text'
        },
        {
          field: 'addr',
          title: '详细地址',
          type: 'text'
        },
        {
          field: 'remarks',
          title: '备注',
          type: 'text'
        }, {
          field: 'flag',
          title: '标志',
          type: 'text',
          isHiden: true,
          hideDetail: true,
          hideEdit: true
        }
      ],


      emoData: {},
      editSubmitButtons: [

        {
          title: '提交',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }
            form.attendanceAddrList = this.editCustomerData

            if (form.status === '1') {
              form.type = '1'
              form.status = '1'
              form.action = 1
              updateBack(form).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
              })
              this.$refs.editRosterRef.hidden()
            } else {
              form.type = '1'
              this.editOutApply(form, '1')
            }
          }
        },
        {
          title: '暂存',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }

            form.attendanceAddrList = this.editCustomerData


            if (form.status === '1') {
              form.type = '1'
              form.status = '0'
              updateBack(form).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
                this.$refs.editRosterRef.hidden()
                this.getList()
              })
            } else {
              form.type = '1'
              this.editOutApply(form, '0')
            }
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.editRosterRef.hidden()
          }
        }
      ],
      addSubmitButtons: [

        {
          title: '提交',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }
            form.attendanceAddrList = this.editCustomerData
            form.type = '1'
            this.addOutApply(form, '1')
          }
        },
        {
          title: '暂存',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }

            form.attendanceAddrList = this.editCustomerData
            form.type = '1'
            this.addOutApply(form, '0')
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.addRosterRef.hidden()
          }
        }
      ],
      editCostSubmitButtons: [

        {
          title: '提交',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }
            if (this.realcustomerData.length === 0) {
              this.$message({
                message: '请选择实际地址',
                type: 'error'
              })
              return
            }
            if (this.costlistData.length === 0) {
              this.$message({
                message: '请选择报销金额',
                type: 'error'
              })
              return
            }
            form.type = '1'
            this.custData = []
            this.custData = this.editCustomerData
            this.submitCustDate = this.custData
            this.realcustomerData.forEach((v, i) => {
              this.submitCustDate.push(v)
            })
            form.attendanceAddrList = this.submitCustDate
            form.costBeanList = this.costlistData
            if (form.reimburseStatuis == undefined) {
              this.addCostSubmit(form)
              return
            }
            if (form.reimburseStatuis === '1') {
              this.updateBackCostFun(form, '1')
            }
            if (form.reimburseStatuis === '0') {
              form.reimburseStatuis = '1'
              sendEditOutApplyApi(form).then(response => {
                if (response.success) {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  })
                  this.$refs.bfEditCost.hidden()
                  this.getList()
                }
              })
            }
          }
        },
        {
          title: '暂存',
          click: (form) => {
            if (this.editCustomerData.length === 0) {
              this.$message({
                message: '请选择目标地址',
                type: 'error'
              })
              return
            }

            if (this.realcustomerData.length === 0) {
              this.$message({
                message: '请选择实际地址',
                type: 'error'
              })
              return
            }
            if (this.costlistData.length === 0) {
              this.$message({
                message: '请选择报销金额',
                type: 'error'
              })
              return
            }
            form.type = '1'
            this.custData = []
            this.custData = this.editCustomerData
            this.submitCustDate = this.custData
            this.realcustomerData.forEach((v, i) => {
              this.submitCustDate.push(v)
            })
            form.attendanceAddrList = this.submitCustDate
            form.costBeanList = this.costlistData

            if (form.reimburseStatuis === '1') {
              this.updateBackCostFun(form, '0')
            } else {
              this.addCostSave(form)
            }
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.bfEditCost.hidden()
          }
        }
      ],
      searchData: '',
      userData: {
      },
      rowAction: [
        {
          label: '详情',
          click: (item) => {
            if (item.scope.row.status === '2' && item.scope.row.reimburseStatuis !== '0') {
              this.settingHis.procDefKey = 'attendance-reimburse'
            } else {
              this.settingHis.procDefKey = 'attendance-outside'
            }
            this.settingHis.businessKey = item.scope.row.outsideId
            this.getOutSideData(item.scope.row.outsideId)
            this.$refs.bfTableDetail.show(item.scope.row)
          }
        },
        {
          label: '编辑',
          hiddenStatus: (row) => {
            if (row.taskDefKey === 'modify' || row.reimburseStatuis === '0') {
              return true
            } else if (row.status === '0') {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            if (item.scope.row.status === '2') {
              if (item.scope.row.taskDefKey === 'modify' || item.scope.row.reimburseStatuis === '0') {
                this.getOutSideData(item.scope.row.outsideId)
                this.$refs.bfEditCost.show(item.scope.row)
              } else {
                this.$message({
                  message: '该状态不可发编辑',
                  type: 'error'
                })
                return
              }
            } else {
              if (item.scope.row.status === '0') {
                // this.$refs.table.showEdit(item)
                this.getOutSideData(item.scope.row.outsideId)
                this.$refs.editRosterRef.show(item.scope.row)
              } else if (item.scope.row.status === '1' && item.scope.row.taskDefKey === 'modify') {
                this.getOutSideData(item.scope.row.outsideId)
                this.$refs.editRosterRef.show(item.scope.row)
              } else {
                this.dealStatus(item.scope.row.status)
              }
            }
          }
        },
        {
          label: '发起报销申请',
          hiddenStatus: (row) => {
            if (row.reimburseStatuis === '2') {
              return false
            }
            if (row.status === '2' && row.taskDefKey === undefined) {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            this.custData = []
            this.realcustomerData = []
            this.costlistData = []
            if (item.scope.row.status === '2' && item.scope.row.taskDefKey === undefined) {
              item.scope.row.type = '1'
              this.getOutSideData(item.scope.row.outsideId)
              this.$refs.bfEditCost.show(item.scope.row)
              // this.sendOutApply(item.scope.row)
            } else {
              this.$message({
                message: '该状态不可发起报销申请',
                type: 'error'
              })
            }
          }
        },
        {
          label: '提交',
          hiddenStatus: (row) => {
            if (row.status === '2' && (row.taskDefKey === 'modify' || (row.reimburseStatuis === '0' || row.reimburseStatuis === undefined))) {
              return true
            } else if (row.status === '0' || row.taskDefKey === 'modify') {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            if (item.scope.row.status === '2' && (item.scope.row.taskDefKey === 'modify' || (item.scope.row.reimburseStatuis === '0' || item.scope.row.reimburseStatuis === undefined))) {
              getOutSideDataApi(item.scope.row.outsideId).then((response) => {
                response.data.type = '1'
                if (response.success) {
                  if (response.data.costBeanList.length === 0) {
                    this.$message({
                      message: '发起报销的报销金额不能为空！',
                      type: 'error'
                    })
                    return
                  }
                  if (response.data.reimburseStatuis === '1') {
                    response.data.reimburseStatuis = '1'
                    response.data.action = '1'
                    updateBackCost(response.data).then((response) => {
                      this.getList()
                    })
                  } else {
                    response.data.reimburseStatuis = '1'
                    response.data.action = '1'
                    sendEditOutApplyApi(response.data).then((response) => {
                      this.getList()
                    })
                  }
                }
              })
            } else if (item.scope.row.status === '0' || item.scope.row.taskDefKey === 'modify') {
              getOutSideDataApi(item.scope.row.outsideId).then((response) => {
                if (response.success) {
                  item.scope.row.attendanceAddrList = response.data.attendanceAddrList

                  if (item.scope.row.status === '0') {
                    this.editOutApply(item.scope.row, '1')
                  } else {
                    item.scope.row.type = '1'
                    item.scope.row.status = '1'
                    item.scope.row.action = 1
                    updateBack(item.scope.row).then((response) => {
                      if (response.success) {
                        this.$message({
                          message: '操作成功',
                          type: 'success'
                        })
                      }
                      this.$refs.editRosterRef.hidden()
                      this.getList()
                    })
                  }
                }
              })
            } else {
              this.dealStatus(item.scope.row.status)
            }
          }
        },
        {
          label: '删除',
          hiddenStatus: (row) => {
            if (row.status === '0'){
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            if (item.scope.row.status === '0') {
              this.deleteOutApply(item.scope.row, '1')
            } else if (item.scope.row.reimburseStatuis === '0') {
              deleteCost(item.scope.row.outsideId).then(response => {
                if (response.success) {
                  this.$message({
                    message: '删除报销申请成功',
                    type: 'success'
                  })
                  this.getList()
                }
              })
            } else {
              this.$message({
                message: '审批中或者审批通过，不可删除',
                type: 'error'
              })
            }
          }
        },
        {
          label: '放弃',
          hiddenStatus: (row) => {
            if (row.status === '1' && row.taskDefKey === 'modify') {
              return true
            } else if (row.status === '2' && row.reimburseStatuis === '1' && row.taskDefKey === 'modify') {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            const obj = {
              outsideId: item.scope.row.outsideId,
              type: '1',
              procInstId: item.scope.row.procInstId,
              taskId: item.scope.row.taskId,
              taskDefKey: item.scope.row.taskDefKey

            }
            if (item.scope.row.status === '1' && item.scope.row.taskDefKey === 'modify') {
              giveupdata(obj).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
                this.getList()
              })
            } else if (item.scope.row.status === '2' && item.scope.row.reimburseStatuis === '1' && item.scope.row.taskDefKey === 'modify') {
              giveupCost(obj).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
                this.getList()
              })
            } else {
              this.$message({
                message: '该状态不可放弃！',
                type: 'error'
              })
              return
            }
          }
        },
      ],
      searchSetting: searchSetting,
      listSetting: listSetting,
      tableListSetting: tableListSetting,
      detailSetting: detailSetting,
      addlistSetting: addlistSetting,
      reimbursementDetailSetting: reimbursementDetailSetting,
      addRules: {
        vacationEndDate: [
          { required: true, message: '请选择出行类型', trigger: 'blur' }
        ],
        outsideType: [
          { required: true, message: '请选择请假类型', trigger: 'blur' }
        ],
        outsideCause: [
          { required: true, message: '请输入申请原因', trigger: 'blur' },
          { min: 3, max: 200, message: '长度在 3 到 200个字符', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        realDestination: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        applyName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ]
      },
      searchParams: { // 搜索条件
        page: 1
      }
    }
  },
  mounted: function() {
    this.getList()

    getEmoApi().then(response => {
      // this.emoData = response.data
      // console.log(response)
      if (response.data) {
        this.emoData = response.data
      } else {
        this.emoData = {}
      }
    })
  },
  methods: {
    // 目标客户
    submitAdd(form) {
      // console.log(form)
      const isPush = true
      // this.editCustomerData.forEach((v, i) => {
      //   if (form.cusNo === v.cusNo) {
      //     this.$message({
      //       message: '该客户已存在！',
      //       type: 'error'
      //     })
      //     isPush = false
      //   }
      // })
      if (isPush) {
        form.flag = 1
        const now = new Date()
        form._addTimeId = now.getTime()
        this.editCustomerData.push(form)
        this.$refs.editDialogFormAdd.hidden()
      }
    },
    submitCustomEdit(form) {
      if (form._addTimeId) {
        this.editCustomerData.forEach((v, i) => {
          if (v._addTimeId == form._addTimeId) {
            this.editCustomerData.splice(i, 1, form)
          }
        })
      }
      if (form.id) {
        this.editCustomerData.forEach((v, i) => {
          if (v.id == form.id) {
            this.editCustomerData.splice(i, 1, form)
          }
        })
      }
      this.$refs.editdialogFormEdit.hidden()
    },
    addCustomer() {
      const initObj = {
        cusName: '',
        cusLinkman: '',
        linkmanTel: '',
        cusAddr: ''
      }
      this.$refs.editDialogFormAdd.show(initObj)
    },



    // 实际客户
    submitRealAdd(form) {
      // console.log(form)
      const isPush = true
      // this.realcustomerData.forEach((v, i) => {
      //   if (form.cusNo === v.cusNo) {
      //     this.$message({
      //       message: '该客户已存在！',
      //       type: 'error'
      //     })
      //     isPush = false
      //   }
      // })
      if (isPush) {
        form.flag = 2
        const now = new Date()
        form._addTimeId = now.getTime()
        this.realcustomerData.push(form)
        this.$refs.editDialogFormRealAdd.hidden()
      }
    },
    submitCustomRealEdit(form) {
      if (form._addTimeId) {
        this.realcustomerData.forEach((v, i) => {
          if (v._addTimeId == form._addTimeId) {
            this.realcustomerData.splice(i, 1, form)
          }
        })
      }
      if (form.id) {
        this.realcustomerData.forEach((v, i) => {
          if (v.id == form.id) {
            this.realcustomerData.splice(i, 1, form)
          }
        })
      }
      // editDialogFormRealAdd
      // editdialogFormRealEdit
      this.$refs.editdialogFormRealEdit.hidden()
    },
    addRealCustomer() {
      const initObj = {
        cusName: '',
        cusLinkman: '',
        linkmanTel: '',
        cusAddr: ''
      }
      this.$refs.editDialogFormRealAdd.show(initObj)
    },

    // 报销
    submitCostAdd(form) {
      // console.log(form)费用类型
      let isPush = true
      this.costlistData.forEach((v, i) => {
        if (form.costDate === v.costDate && v.costType === form.costType) {
          this.$message({
            message: '今天已存在该类型的报销！',
            type: 'error'
          })
          isPush = false
        }
      })
      if (isPush) {
        form.flag = 1
        const now = new Date()
        form._addTimeId = now.getTime()
        this.costlistData.push(form)
        this.$refs.editDialogFormCostAdd.hidden()
      }
    },
    submitCostEdit(form) {
      if (form._addTimeId) {
        this.costlistData.forEach((v, i) => {
          if (v._addTimeId == form._addTimeId) {
            this.costlistData.splice(i, 1, form)
          }
        })
      }
      if (form.id) {
        this.costlistData.forEach((v, i) => {
          if (v.id == form.id) {
            this.costlistData.splice(i, 1, form)
          }
        })
      }
      // editDialogFormRealAdd
      // editdialogFormRealEdit
      this.$refs.editdialogFormCostEdit.hidden()
    },

    addCost() {
      const initObj = {
        costDate: '',
        costType: '',
        cost: ''
      }
      this.$refs.editDialogFormCostAdd.show(initObj)
    },

    addCostSubmit(form) {
      form.reimburseStatuis = '1'
      form.action = 1
      form.type = '1'

      sendOutApplyApi(form).then(response => {
        this.$refs.bfEditCost.hidden()
        this.getList()
      })
    },



    addCostSave(form) {
      form.reimburseStatuis = '0'
      form.action = 1
      form.type = '1'
      sendEditOutApplyApi(form).then(response => {
        this.$refs.bfEditCost.hidden()
        this.getList()
      })
    },


    getOutSideData(id) {
      getOutSideDataApi(id).then(response => {
        // console.log(response)
        this.editCustomerData = []
        this.realcustomerData = []
        this.costlistData = []
        if (response.data && response.data.attendanceAddrList) {
          response.data.attendanceAddrList.forEach(v => {
            if (v.flag == '1') {
              this.editCustomerData.push(v)
            }
            if (v.flag == '2') {
              this.realcustomerData.push(v)
            }
          })
        }
        if (response.data && response.data.costBeanList) {
          this.costlistData = response.data.costBeanList
        }
      })
    },
    editCustomer() {

    },
    deleteOutApply(form) {
      // console.log(form)
      deleteOutApplyApi(form.outsideId).then(response => {
        if (response.success) {
          this.$message({
            message: '删除出行申请成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    addOutApply(form, status) {
      form.status = status
      form.employeeNo = this.emoData.employeeNo
      form.employeeName = this.emoData.employeeName
      form.orgId = this.emoData.orgId
      form.position = this.emoData.position
      form.type = '1'
      addOutApplyApi(form).then(response => {
        if (response.success) {
          this.$message({
            message: '新增出行申请成功',
            type: 'success'
          })
          this.$refs.addRosterRef.hidden()
          this.getList()
        }
      })
    },
    /**
     * 退回修改或者提交
     */

    updateBackCostFun(form, reimburseStatuis) {
      form.action = 1
      form.reimburseStatuis = reimburseStatuis
      form.employeeNo = this.emoData.employeeNo
      form.employeeName = this.emoData.employeeName
      form.orgId = this.emoData.orgId
      form.position = this.emoData.position
      form.type = '1'
      updateBackCost(form).then(response => {
        if (response.success) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$refs.bfEditCost.hidden()
          this.getList()
        }
      })
    },

    sendOutApply(form) {
    },
    dealStatus(status) {
      if (status === '1') {
        this.$message({
          message: '出行申请审批中，请勿重复提交或者编辑',
          type: 'error'
        })
        return
      }
      if (status === '2') {
        this.$message({
          message: '出行申请审批已经通过，请勿重复提交或者编辑',
          type: 'error'
        })
        return
      }
    },
    getList() {
      // if (!params) {
      //   params = {
      //     page: 1,
      //   }
      // }

      // const type = {
      //   type: '1'
      // }

      // const obj = Object.assign(params, type)
      const params = this.searchParams
      params.type = 1
      getListApi(params).then(response => {
        this.userData = response
      })
    },
    searchList(form) {
    },
    addSubmit(form) {
      // console.log(form)
    },
    searchRosterSubmit(form) {
      // console.log(form)
      // form.page = 1
      // this.getList(form)
      // console.log(form)
      this.searchParams.page = 1
      this.searchParams.status = form.status
      this.searchParams.applyType = form.applyType
      this.searchParams.employeeName = form.employeeName
      this.getList()
      this.$refs.searchRosterRef.hidden()
    },
    editRosterSubmit(form) {
    },
    reimbursementEditSubmit() {

    },
    editOutApply(form, status) {
      /**
       * 编辑申请
       */
      form.status = status
      editOutApplyApi(form).then(response => {
        if (response.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.$refs.editRosterRef.hidden()
          this.getList()
        }
      })
    },
    addFree() { },
    pageChange(pageObj) {
      this.searchParams.page = pageObj.page
      this.searchParams.pageSize = pageObj.pageSize
      this.getList()
    },
    showSearch() {
      this.$refs.searchRosterRef.show()
    },
    showAdd() {
      this.realcustomerData = []
      this.costlistData = []
      this.editCustomerData = []
      this.$refs.addRosterRef.show()
    },
    quickSearch() {
      // const params = {
      //   applyName: this.searchData
      // }
      this.searchParams.page = 1
      this.searchParams.applyName = this.searchData
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
